﻿using IntelligentGateway.Model.Panel;
using IntelligentGateway.Tools;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntelligentGateway.Services.Panel
{
    public class SyncDataServices
    {
        public async Task<string> Login(string UserName, string Password, string PanelIP)
        {
            string urlPath = "http://" + PanelIP + @"/api/system/login";
            PanelLoginRequest login = new PanelLoginRequest();
            login.@params = new LoginInfo();
            login.@params.UserName = UserName;
            login.@params.Password = UTools.MD5(Password).ToUpper();
            login.@params.ForceLogin = 1;
            string jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<PanelLoginRequest>(urlPath, login);
            PanelResponseMessage response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            if (response != null)
            {                
                return response.@params.Token;
            }
            else return "";
        }
        public async Task<PanelResponseMessage> SavePerson(SavePerson Info,string PanelIP)
        {
            string urlPath = "http://" + PanelIP + @"/api/person/insertPerson";         
            Info.Token = GlobalVariables.Token;
            string jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePerson>(urlPath, Info);
            PanelResponseMessage response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            if (response != null)
            {
                if (response.result == 108)
                {
                    string token = await Login(GlobalVariables.SyncPanelUser, GlobalVariables.SyncPanelPassword, PanelIP);
                    if (string.IsNullOrEmpty(token))
                    {
                        return response;
                    }
                    else
                    {
                        GlobalVariables.Token = token;
                    }
                    Info.Token = GlobalVariables.Token;
                    jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePerson>(urlPath, Info);
                    response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
                }
                if (response.result == 504)//TODO 待定
                {
                    Info.Token = GlobalVariables.Token;
                    urlPath = "http://" + PanelIP + @"/api/person/updatePerson";
                    jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePerson>(urlPath, Info);
                    response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);                    
                    return response;
                }
                else
                    return response;

            }
            else
            {
                response =new PanelResponseMessage();
                response.method = "savePerson";
                response.result = 1;
                return response;
            }
            
        }
        public async Task<PanelResponseMessage> DeletePerson(SavePerson Info, string PanelIP)
        {
            string urlPath = "http://" + PanelIP + @"/api/person/deletePerson";
            string jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePerson>(urlPath, Info);
            PanelResponseMessage response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            if (response.result == 108)
            {
                string token = await Login(GlobalVariables.SyncPanelUser, GlobalVariables.SyncPanelPassword, PanelIP);
                if (string.IsNullOrEmpty(token))
                {
                    return response;
                }
                else
                {
                    GlobalVariables.Token = token;
                }
                Info.Token = GlobalVariables.Token;
                jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePerson>(urlPath, Info);
                response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            }
            
            return response;
        }
        public async Task<PanelResponseMessage> SetCallBack(SetCallBack Info, string PanelIP)
        {
            string urlPath = "http://" + PanelIP + @"/api/person/setCallBack";
            string jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SetCallBack>(urlPath, Info);
            PanelResponseMessage response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            return response;
        }
        public async Task<PanelResponseMessage> SavePersonPicture(SavePersonPicture Info, string PanelIP)
        {
            string urlPath = "http://" + PanelIP + @"/api/person/savePersonPicture";
            string jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePersonPicture>(urlPath, Info);
            PanelResponseMessage response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            if (response!=null && response.result == 108)
            {
                string token = await Login(GlobalVariables.SyncPanelUser, GlobalVariables.SyncPanelPassword, PanelIP);
                if (string.IsNullOrEmpty(token))
                {
                    return response;
                }
                else
                {
                    GlobalVariables.Token = token;
                }
                Info.Token = GlobalVariables.Token;
                jsonData = await Tools.HttpTools.HttpClientPostSyncDataString<SavePersonPicture>(urlPath, Info);
                response = JsonConvert.DeserializeObject<PanelResponseMessage>(jsonData);
            }
            return response;
        }
    }
}
